package com.fanrenqiji.common.factory;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年4月9日 下午3:37:44
 * @description
 */
public class ObjectCombineFactory {
	/**
	 * 此处用在更新操作中合并combine直接是操作类对象 慎用
	 * 
	 * @param source
	 *            是需要进行合并的源对象（不是持久对象）
	 * @param dest
	 *            目标数据（当前传递过来的数据）当做merge后的对象
	 * @throws IntrospectionException
	 *             , IllegalAccessException,InvocationTargetException
	 * @autor lijianzhen
	 */
	public synchronized static void combine(Object source, Object dest)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		// 获取属性
		BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(),
				java.lang.Object.class);
		PropertyDescriptor[] sourceProperty = sourceBean
				.getPropertyDescriptors();

		BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(),
				java.lang.Object.class);
		PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();

		for (int i = 0; i < sourceProperty.length; i++) {
			String value = String.valueOf(sourceProperty[i].getReadMethod()
					.invoke(source));
			for (int j = 0; j < destProperty.length; j++) {
				if (null == sourceProperty[i].getReadMethod().invoke(source)
						|| "null".equals(value) || "".equals(value.trim())) {
					break;
				}
				if (sourceProperty[i].getName().equals(
						destProperty[j].getName())) {
					destProperty[j].getWriteMethod().invoke(dest,
							sourceProperty[i].getReadMethod().invoke(source));
					break;
				}
			}
		}

	}
}
